package cn.smbms.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sun.istack.internal.logging.Logger;

import cn.smbms.pojo.Provider;
import cn.smbms.pojo.Role;
import cn.smbms.pojo.User;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.service.user.UserService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;
import cn.smbms.tools.ResponseResult;
import cn.smbms.tools.ResultCode;


@Controller
@RequestMapping("/provider")
public class ProviderController {
	
	Logger logger = Logger.getLogger(ProviderController.class);
	
	@Resource
	private ProviderService providerService;
	
	//分页查询供应商列表  ???????????????????????????
	@RequestMapping("/providerlist")
	@ResponseBody
	public Object getProviderList(@RequestParam(required=false)String proCode,
								@RequestParam(required=false)String proName,
								@RequestParam(required=false)Integer pageIndex,
								HttpSession session) throws Exception{
		User user = (User) session.getAttribute(Constants.USER_SESSION);
		//判断用户是否登录
		if(user == null){
			return ResponseResult.failed(ResultCode.ERROR_700.code(), ResultCode.ERROR_700.message());
		}
		
		int pageSize = Constants.pageSize;  //页容量
		
		//总数量
		int totalCount = providerService.getProviderCount(proCode, proName);
		
		//控制首页
		if(pageIndex == null || pageIndex < 1){
			pageIndex = 1;
		}
		
		PageSupport<Provider> page=new PageSupport<Provider>();
		page.setCurrentPageNo(pageIndex);
		page.setPageSize(pageSize);
		page.setTotalCount(totalCount);
		//总页数
		int totalPageCount =page.getTotalPageCount();
		//控制尾页
		if(pageIndex > totalPageCount){ 
			pageIndex = totalPageCount;
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("proCode", proCode);
		map.put("proName", proName);
		map.put("currentPageNo",pageIndex);
		map.put("pageSize",pageSize);
		//调用service层获取数据
		List<Provider> proList=providerService.getProviderListByPages(map);
		
		Map<String,Object> map2 = new HashMap<String, Object>();
		//封装数据返回前端
		map2.put("data",proList);
		map2.put("proCode", proCode);
		map2.put("proName", proName);
		return ResponseResult.ok(map2, "数据查询成功");
	}

	
	//增加供应商
	@ResponseBody
	@RequestMapping(value="/provideraddsave")
	public Object addProviderSave(Provider provider,HttpSession session,
								HttpServletRequest request) throws Exception{
	
		User user2 = (User) session.getAttribute(Constants.USER_SESSION);
		//判断用户是否登录
		if(user2 == null){
			return ResponseResult.failed(ResultCode.ERROR_700.code(), ResultCode.ERROR_700.message());
		}
		provider.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		provider.setCreationDate(new Date());
		
		if(providerService.add(provider)>0){
			return ResponseResult.ok("供应商添加成功！");
		}else{
			return ResponseResult.failed(ResultCode.ERROR_SQL_ERROR.code(),ResultCode.ERROR_SQL_ERROR.message());
		}
		
		
	}

	//修改供应商
	@RequestMapping(value="/providermodifysave")
	@ResponseBody
	public Object modifyProviderSave(Provider provider,HttpSession session) throws NumberFormatException, Exception{
		User user2 = (User) session.getAttribute(Constants.USER_SESSION);
		//判断用户是否登录
		if(user2 == null){
			return ResponseResult.failed(ResultCode.ERROR_700.code(), ResultCode.ERROR_700.message());
		}
		provider.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		provider.setModifyDate(new Date());
		logger.info("userid-------------"+provider.getId());
		if(providerService.modify(provider)>0){
			return ResponseResult.ok("修改成功");
		}else{
			return ResponseResult.failed("修改失败");
		}
	}
	
	//查看供应商
	@RequestMapping(value="/view")
	@ResponseBody
	public Object view(@RequestParam(required=false) String id,
						HttpSession session) throws NumberFormatException, Exception{
		User user2 = (User) session.getAttribute(Constants.USER_SESSION);
		//判断用户是否登录
		if(user2 == null){
			return ResponseResult.failed(ResultCode.ERROR_700.code(), ResultCode.ERROR_700.message());
		}
		Provider provider = null;
		if(id != null && !id.equals("")){
			provider = providerService.getProviderById(Integer.parseInt(id));
		}
		return ResponseResult.ok(provider);
	}
	
	//删除供应商
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestParam(required=false) String id,
							HttpSession session) throws Exception{
		User user2 = (User) session.getAttribute(Constants.USER_SESSION);
		//判断用户是否登录
		if(user2 == null){
			return ResponseResult.failed(ResultCode.ERROR_700.code(), ResultCode.ERROR_700.message());
		}
		Integer _id = null;
		if(id != null && !id.equals("")){
			_id = Integer.parseInt(id);
		}
		if(providerService.deleteProviderById(_id)>0){
			return ResponseResult.ok("删除供应商成功");
		}else{
			return ResponseResult.failed("删除供应商失败");
		}
		
	}
}
